Unit
	WebApplication;

Interface

Uses
	Classes,
	SysUtils,
	PWInit,
	PWMain,
	MD5,
	RTTIObject,
	ComponentLoader;

Const
	ccSessionCookie = 'session';
	ccSessionDir = '.\sessions\';

Type
	TWebApplication = Class
	Private
		fSession,
		fRootNode,
		fFileName : String;
		fAborted : Boolean;
	Public
		Constructor Create(Const aDefault : String);
		Destructor Destroy; Override;
		Procedure Abort;
		Procedure Load;
		Procedure Run;
		Property RootNode : String Read fRootNode Write fRootNode;
		Property Session : String Read fSession Write fSession;
		Property FileName : String Read fFileName Write fFileName;
	End;
	
Var
	Application : TWebApplication;

Implementation

Constructor TWebApplication.Create(Const aDefault : String);
Begin
	Inherited Create;
	fAborted := False;
	If IsCookie(ccSessionCookie) And FileExists(ccSessionDir + ExtractFileName(GetCookie(ccSessionCookie) + '.pon')) Then
	Begin
		fSession := GetCookie(ccSessionCookie);
		fFileName := ccSessionDir + ExtractFileName(fSession + '.pon');
	End
	Else
	Begin
		fSession := Factory.UniqueID;
		fFileName := aDefault + '.pon';
	End;
	Load;
End;

Destructor TWebApplication.Destroy;
Begin
	If Not(fAborted) And Assigned(Factory.FindInstance(fRootNode)) Then
		SaveComponents(ccSessionDir + ExtractFileName(fSession + '.pon'), Factory.FindInstance(fRootNode));
	Inherited Destroy;
End;

Procedure TWebApplication.Abort;
Begin
	fAborted := True;
End;

Procedure TWebApplication.Load;
Begin
	fRootNode := LoadComponents(fFileName);
End;

Procedure TWebApplication.Run;
Begin
	If Not(fAborted) And Assigned(Factory.FindInstance(fRootNode)) Then
	Begin
		SetCookie(ccSessionCookie, fSession);
		Factory.FindNameless;
		Factory.FindInstance(fRootNode).Submit;
		Factory.FindInstance(fRootNode).Process;
		SetWebVar('applicationname', ApplicationName);
		OutFF(Factory.FindInstance(fRootNode).Show);
	End;
End;

End.